<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,maximum-scale=1.0" />
    <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/public.js" type="text/javascript" charset="utf-8"></script>
	<title></title>
	<!-- 发票抬头-编辑 -->
</head>
<body>
	<div id="invoiceEdit">
		<div class="addHeader">
			<span class="back"></span>
			<span class="title">发票抬头</span>
		</div>
		<div class="editCon">
			<div class="editTitle">
				<span class="title">发票抬头</span>
				<span class="name">
					<input type="text" style="border: none;line-height: 1.173rem;text-align: right;" id="name2" name="name2" readonly="readonly" value="个人" >
				</span>
			</div>
			<div class="editlist">
				<span class="listTitle">发票抬头类型</span>
				<div class="listTab">
					<span id="1">个人</span>
					<span id="2">企业</span>
				</div>
			</div>
			<div class="editlist">
				<span class="listTitle">服务内容</span>
				<div id="service" style="float: right">
					<input type="radio" name ="service" value="服务费" checked>服务费
					<input type="radio" name ="service" value="网络建设" >网络建设
				</div>
			</div>
			<div class="editChange">
				<div class="changeList personal">
					<div class="list">
						<span class="listTitle">发票类型</span>
						<span class="listName">增值税普通发票</span>
					</div>

				</div>
				<div class="changeList enterprise" style="display: none;">
					<div class="list">
						<span class="listTitle">发票类型</span>
						<span class="listName">企业增值税普通发票</span>
					</div>
					<div class="list">
						<span class="listTitle">
							统一社会信用代码
							<!--税务登记证号-->
						</span>
						<input type="text" name="swdjzh" class="swdj" value="" placeholder="请填写您纳税登记证上的编号"/>
					</div>
					<div class="list">
						<span class="listTitle">注册场所地址</span>
						<input type="text" name="zccsdz" class="zccs" value="" placeholder="请填写您营业执照上的注册地址"/>
					</div>
					<div class="list">
						<span class="listTitle">注册固定电话</span>
						<input type="text" name="zcgddh" class="zcgddh" value="" placeholder="请填写您公司有效的联系电话"/>
					</div>
					<div class="list">
						<span class="listTitle">开户银行</span>
						<input type="text" name="khyh" class="khyh" value="" placeholder="请填写您开户许可证上的开户银行"/>
					</div>
					<div class="list">
						<span class="listTitle">银行账号</span>
						<input type="text" name="yhzh" class="yhzh" value="" placeholder="请填写您开户许可证上的银行账号"/>
					</div>
				</div>
			</div>
			<a href="javascript:void(0);" class="submitBtn" onclick="subKeep()">保存</a>
		</div>
		<span id="tishi" style="float: right; margin: 20px;color: red"></span>
	</div>
	<!--<div class="mask"></div>-->
	<!--<div class="enterCon">-->
		<!--<div class="conlist ttt" id="2">-->
			<!--企业增值税普通发票-->
		<!--</div>-->
		<!--<div class="conlist" id="3">-->
			<!--增值税专发票-->
		<!--</div>-->
		<!--<div class="conlist" id="4">-->
			<!--组织增值税普通发票-->
		<!--</div>-->
		<!--<span class="cancel">取消</span>-->
	<!--</div>-->
	<script>
        $(function(){
            //验证是否登陆
            var user_id = sessionStorage.getItem('userId');
            if (user_id == null) {
                alert('请先登录！');
                window.location.href = 'login.html';
            }
            //获取抬头
            getRise2();
        });
        function getRise2(){
            var user_id = sessionStorage.getItem('userId');
            if (sessionStorage.getItem('invoceId') != null) {
                $.ajax({
                    url: url() + 'api/Invoice/invoiceHeader',    //请求的url地址
                    dataType: "JSON",   //返回格式为json
                    async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                    type: "POST",   //请求方式
                    data: {
                        user_id:user_id,
                    },
                    success: function (data) {
                        // console.log(data);
                       if (data.result.service == '服务费') {
                           var hh = `
                        		<input type="radio" name ="service" value="服务费" checked>服务费
								<input type="radio" name ="service" value="网络建设" >网络建设
                        `;
					   } else {
                           var hh = `
                        		<input type="radio" name ="service" value="服务费">服务费
								<input type="radio" name ="service" value="网络建设"  checked>网络建设
                        `;
					   }
                        $('#service').html(hh);
                        if (data.result.header_name == '个人') {
                            var index=$('.listTab span').index();
                            var html ='<input type="text" style="border: none;line-height: 1.173rem;text-align: right;" id="name2" name="name2" readonly="readonly" value="个人" >';
                            $('.editTitle .name').html(html);
							$('.editChange .changeList').eq(index).show().siblings().hide();
							$('#1').addClass('tabOn').siblings().removeClass('tabOn');
						} else {
                            var index = $('.listTab span')[0].id;
                            var html ='<input type="text" style="border: none;line-height: 1.173rem;text-align: right;" id="name2" name="name2" value='+data.result.header_name+' placeholder="请填写企业名称">';
                            $('.editTitle .name').html(html);
                            $('.listTab span').addClass('tabOn').siblings().removeClass('tabOn');
							$('.editChange .changeList').eq(index).show().siblings().hide();
							$('#2').addClass('tabOn').siblings().removeClass('tabOn');
                            var swdjzh = $('.swdj').val(data.result.tax_number);
                            var zccs = $('.zccs').val(data.result.address);
                            var zcgddh = $('.zcgddh').val(data.result.fixed_telephone);
                            var khyh = $('.khyh').val(data.result.bank);
                            var yhzh = $('.yhzh').val(data.result.bank_card);
						}
                    }
                });
			}
		};
	</script>
<script type="text/javascript">
	$(".back").click(function(){
		history.go(-1)
	})
	//切换tab
	$('.listTab span').on('click',function(){
		var index=$(this).index();
		var id = $(this)[0].id;
		if(id==1){
            var html ='<input type="text" style="border: none;line-height: 1.173rem;text-align: right;" id="name2" name="name2" readonly="readonly" value="个人" >';
		} else {
            var html ='<input type="text" style="border: none;line-height: 1.173rem;text-align: right;" id="name2" name="name2" value="" placeholder="请填写企业名称">';
		}
		$('.editTitle .name').html(html);
		$(this).addClass('tabOn').siblings().removeClass('tabOn');
		$('.editChange .changeList').eq(index).show().siblings().hide()
	});
	//点击弹出发票类型选项
	$('.enterprise .listName').on('click',function(){
		$('.enterCon').animate({
			'height':'5.466rem'
		},200);
		$('.mask').animate({
			'height':'100%'
		},200).show();
		//点击遮罩，取消
		$('.mask , .cancel').on('click',function(){
			$('.enterCon').animate({
				'height':'0'
			},200);
			$(this).animate({
				'height':'0'
			},200).hide()
		})
	});
	//选中发票类型，并传回值
	$('.enterCon .conlist').on('click',function(){
		var html1=$(this).html();
		$('.enterprise .listName').html(html1);
        $(this).addClass('ttt').siblings().removeClass('ttt');
        var inType = $('.enterCon .ttt')[0].id;
        if (inType==3) {
            $('#tishi').html('以上信息请全部填写，一便开取增值税专用发票');
		}
		$('.enterCon').animate({
			'height':'0'
		},200);
		$('.mask').animate({
			'height':'0'
		},200).hide()
	});

	//提交抬头信息
	function subKeep() {
        var service = $("input[name='service']:checked").val();
	    var user_id = sessionStorage.getItem('userId');
        var name = $('.editTitle .name #name2').val();
        var type = $('.listTab .tabOn')[0].id;
        var swdjzh = $('.swdj').val();
        var zccs = $('.zccs').val();
        var zcgddh = $('.zcgddh').val();
        var khyh = $('.khyh').val();
        var yhzh = $('.yhzh').val();

        if (type==1){
            var invoiceType = 1;
		}
        if (type==2) {
            if (name=='') {
                alert('请填写企业名称');
                return false;
			}
            if (swdjzh=='') {
                alert('请填写统一社会信用代码');
                return false;
            }
            invoiceType = 2;
            // invoiceType = $('.enterCon .ttt')[0].id;
            // if (invoiceType==3) {
            //     if (zccs=='') {
            //         alert('请填写注册场所地址');
            //         return false;
            //     } if (zcgddh=='') {
            //         alert('请填写注册固定电话');
            //         return false;
            //     } if (khyh=='') {
            //         alert('请填写开户银行');
            //         return false;
            //     } if (yhzh=='') {
            //         alert('请填写银行账号');
            //         return false;
            //     }
			// }
		}
        if (sessionStorage.getItem('invoceId') != null) {
            var invoceId = sessionStorage.getItem('invoceId');
		} else {
            invoceId = '';
		}
        $.ajax({
            url: url() + 'api/Invoice/invoceAddEdit', //请求的url地址
            dataType: "JSON", //返回格式为json
            async: true, //请求是否异步，默认为异步，这也是ajax重要特性
            type: "POST", //请求方式
			data: {
                invoce_id:invoceId,
                user_id:user_id,
                header_name:name,
                invoice_type:invoiceType,
                tax_number:swdjzh,
                address:zccs,
                fixed_telephone:zcgddh,
                bank:khyh,
                bank_card:yhzh,
                service:service,
			},
            success: function (data) {
                console.log(data);
                // 请求成功时处理
                if(data.status==1){
                    window.location.href = 'invoiceTitle.html';
                } else {
                    alert(data.msg);
                }
            }
        });
    }
</script>
</body>
</html>
